1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class MusicController : MonoBehaviour {
6     
public static MusicController instance;
7
8     
[HideInInspector]
9     
public AudioSource audioSource;
10
11     
public AudioClip background, gameplay, explode, coin, bossExplode;
12
13     
void Awake(){
14         audioSource = GetComponent<AudioSource> ();
15         CreateInstance ();
16     }
17
18     
// Use this for initialization
19     
void Start () {
20         
21     }
22
23     
void CreateInstance(){
24         
if (instance != null) {
25             Destroy (gameObject);
26         }
else {
27             instance =
this;
28             DontDestroyOnLoad (gameObject);
29         }
30     }
31
32     
public void PlayBackgroundSound(){
33         
if(background){
34             audioSource.clip = background;
35             audioSource.volume =
0.5f;
36             audioSource.loop =
true;
37             audioSource.Play ();
38         }
39     }
40
41     
public void PlayGameplaySound(){
42         
if(gameplay){
43             audioSource.clip = gameplay;
44             audioSource.volume =
0.5f;
45             audioSource.loop =
true;
46             audioSource.Play ();
47         }
48     }
49
50     
public void StopAllSound(){
51         
if(audioSource.isPlaying){
52             audioSource.Stop ();
53         }
54     }
55
56     
public void PlayerDeath(){
57         
if(explode){
58             audioSource.PlayOneShot (explode);
59         }
60     }
61         
62
63 }


Use this for initialization



Gõ tìm kiếm nhanh...